# -*- coding: utf-8 -*-
import os
import json

from abc import ABC

from app.configs import piece_path
from app.views import CommonHandler
from app.models.cod import Error


class TinyHandler(CommonHandler, ABC):
    async def get(self):
        meta_id = self.get_argument("meta_id", None)
        if meta_id is None:
            await self.response(error=Error.NotMeta)
            return

        file = piece_path + meta_id + ".json"
        if not os.path.exists(file):
            await self.response(error=Error.NotMeta)
            return

        with open(file, 'r') as f:
            content = f.read()
            data = json.loads(content)
            await self.response(data=data)
